package warmachineroster.presentation;

import org.eclipse.gef.EditDomain;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.IPageSite;

import warmachineroster.Roster;
import warmachineroster.presentation.dnd.OutlineDragSourceListener;
import warmachinesystem.GameSystem;
import warmachinesystem.presentation.editpart.WarmachineSystemTreeEditPartFactory;

public class WarmachineRosterOutlinePage extends ContentOutlinePage {

	private Roster model;
	private EditDomain editDomain;
	private ActionRegistry actionRegistry;
	


	public WarmachineRosterOutlinePage(EditPartViewer viewer) {
		super(viewer);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		// create outline viewer page
		super.createControl(parent);
		// configure outline viewer
		getViewer().setEditDomain(getEditDomain());
		getViewer().setEditPartFactory(new WarmachineSystemTreeEditPartFactory(model));
	
		// initialize outline viewer with model
		getViewer().setContents(getGameSystem());
		// show outline viewer
		OutlineDragSourceListener listener = new OutlineDragSourceListener(getViewer());
		getViewer().addDragSourceListener(listener);
	}
	
	private EditDomain getEditDomain() {
		return editDomain;
	}
	
	public void setEditDomain(EditDomain ed){
		this.editDomain = ed;
	}

	private Roster getModel() {
		return model;
	}
	
	private GameSystem getGameSystem(){
		return getModel().getArmyList().getGameSystem();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.IPage#dispose()
	 */
	public void dispose() {
		super.dispose();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.IPage#getControl()
	 */
	public Control getControl() {
		return getViewer().getControl();
	}
	
	/**
	 * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
	 */
	public void init(IPageSite pageSite) {
		super.init(pageSite);
		ActionRegistry registry = getActionRegistry();
		IActionBars bars = pageSite.getActionBars();
		String id = ActionFactory.UNDO.getId();
		bars.setGlobalActionHandler(id, registry.getAction(id));
		id = ActionFactory.REDO.getId();
		bars.setGlobalActionHandler(id, registry.getAction(id));
		id = ActionFactory.DELETE.getId();
		bars.setGlobalActionHandler(id, registry.getAction(id));
	}

	private ActionRegistry getActionRegistry() {
		return actionRegistry;
	}

	public ISelection getSelection() {
		return null;
	}
	
	public void setContent(Roster roster) {
		
		model = roster;
	}
	
	public void setActionRegistry(ActionRegistry actionRegistry) {
		this.actionRegistry = actionRegistry;
	}
	
}
